package com.yuer.tools.myfileziptools.controller;

import com.yuer.tools.myfileziptools.service.impl.TestServiceImpl;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

@Controller
public class Test {

    @GetMapping("/hello")
    @ResponseBody
//    @GetMapping
    public String  getHello(){
        return "hello";
    }
    @GetMapping("downloadWord")
    public void downloadWord(HttpServletRequest request, HttpServletResponse response){
        try {
            // 标题
            String title = "测试标题";
            // 内容
            String text = "测试内容";
            //富文本内容
            String content="<html><body>" +
                    "<p style=\"text-align: center;\"><span style=\"font-family: 黑体, SimHei; font-size: 24px;\">"
                    + title + "</span></p>" + text + "</body></html>";
            //这里是必须要设置编码的，不然导出中文就会乱码。
            byte b[] = content.getBytes("GBK");
            //将字节数组包装到流中
            ByteArrayInputStream bais = new ByteArrayInputStream(b);

            /**
             * 关键地方
             * 生成word格式
             */
            POIFSFileSystem poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            directory.createDocument("WordDocument", bais);

            /**
             * 方式一：创建文件并将流输出到文件中
             */
            FileCopyUtils.copy(poifs.createDocumentInputStream("WordDocument"),new FileOutputStream("d:/test.doc"));

            /**
             * 方式二：直接输出流文件下载
             */
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");//导出word格式
            response.addHeader("Content-Disposition", "attachment;filename=" +
                    new String(title.getBytes("GB2312"),"iso8859-1") + ".doc");
            ServletOutputStream ostream = response.getOutputStream();
            poifs.writeFilesystem(ostream);

            bais.close();
            ostream.close();
            poifs.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
@Autowired(required = false)
private RestTemplate restTemplate;

    @GetMapping("/ss")
    @ResponseBody
    public void ss(){
        String url = "https://pan.baidu.com/share/set?channel=chunlei&clienttype=0&web=1&channel=chunlei&web=1&app_id=250528&bdstoken=d99897b441e99c99c1d05fa2318f289b&logid=MkIwQjYzNjUzRjQ5Q0UyNjZBMzBDMUI3RThGNTVCMUI6Rkc9MQ==&clienttype=0";
        String Cookie = "BIDUPSID=C107DD58BA7F4406C4E1C38D0A299CAC; PSTM=1621224161; BAIDUID=2B0B63653F49CE266A30C1B7E8F55B1B:FG=1; __yjs_duid=1_c035cc1fd39147e3d4bbf5a6406ea6391621227983868; PANWEB=1; Hm_lvt_fa0277816200010a74ab7d2895df481b=1623506080; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDCLND=lCtgOnFIFT%2FaG4mJtWuB5U3381l5oyJ7; H_PS_PSSID=31254; BDSFRCVID=-w-OJeC62GYxEBneY9ATEXFroDl7RB5TH6ao8Nu7THxj9j4GJHeOEG0P8M8g0Kub9LTFogKK0mOTHv-F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tbu8VC--tDD3j45NM4r_q4tHeUQ4BMRZ5mAqoD31aq38VJ5nLPriQqkzKxn7apjfWI7naIQqabvHjMT5Wl0234rW3prUBxj43bRTKlCy5KJvfJo9jbbqhP-UyN3LWh37btjlMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafD_bhD-4Djt2eP00-xQja--XKKj2WROeajrjDnCrebAWXUI8LUc72Uvw25Tn0RbNbbnpohQ4hPovyT8sXnO72P7XtHryhIOt2T6JHlvKy4oTjxL1Db3JKjvMtg3t3qQmLUooepvoD-Jc3MvBy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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHoD32B6A99gT; STOKEN=052b823de7e6f2bd256b2e435e38937be52f459f3a5d870c3835c38f6039cfa9; PSINO=2; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1625302678; BA_HECTOR=840l0gal208kag24g21ge0d0q0q; PANPSC=11872281941317594083%3ApdX0uBsTAWsG1gSD60X4BFcS2d9ns3O5g0mIZdLHpdQGbqupDlB1gv7lnPAFdJ9wILn0fuyMgq%2FiLaLUl0KD5De4ZdaW7CoOlL98c8Ccr9ch6uZoP3DwQ9YfJggg9xZJBrU3YVu%2FDjttBW2WG4wtcMtzkM24WL1TQvhxt2JuFrC%2FzVpii0tk4cOsn6fUZl24joV20S1sm8j0Qdghmy%2F7qBg%2B4ZqFzc4WIpE3ZFG2ujN%2F0Yf%2FyAhUnJKxVlqrIDxM";
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("channel_list", new ArrayList<String>().toString());
        map.add("period", 0 + "");
        map.add("pwd", "1234");
        map.add("fid_list", new String []{"1003100670099209"}.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cookie",Cookie);
        HttpEntity<String> request = new HttpEntity(map , headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, request, JSONObject.class);
        System.out.println(responseEntity.toString());
    }
    @Autowired
    TestServiceImpl testService;

    @GetMapping("/ssTest")
    @ResponseBody
    public String ssTest(){
//          testService.sayHerllo();
//        System.out.println("12345678iop");
//        return "1234";
        return  testService.run();
    }


}
